package demo2;

/**
 * Created with IntelliJ IDEA.
 * Description:
 * User: 石方旭
 * Date: 2022-07-25
 * Time: 14:35
 */
public class TestDemo1 {

    public static void main(String[] args) {
        Thread t = new Thread(()->{
            while(!Thread.currentThread().isInterrupted()){
                try {
                    System.out.println("线程运行中...");
                    Thread.sleep(1000);
                } catch (InterruptedException e) {
                    //[1].立即退出
                    //break;
                    //[2]. 等待2s在退出
                    try {
                        System.out.println("该线程即将退出!!!");
                        Thread.sleep(20000);
                    } catch (InterruptedException ex) {
                        ex.printStackTrace();
                    }
                    //[3] 什么也不做
                }

            }
        });

        t.start();
        try {
            Thread.sleep(5000);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        /**
         * 这里的interrupt方法没有遇到阻塞情况时:   ---->会设置标志位
         * interrupt方法遇到阻塞情况时 : 就会让处在阻塞的方法触发异常例如sleep
         */
        t.interrupt();

    }


}
